package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.RoleService;
import com.itheima.vo.RoleVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
@Api(value = "后台管理模块",description = "角色相关接口")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色信息
     * @param roleVo
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody RoleVo roleVo){
        try {
            roleService.add(roleVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","添加失败");
        }
        return new Result("success","添加成功");
    }

    /**
     * 删除角色信息
     * @param roleVo
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Result delete(@RequestBody RoleVo roleVo){
        try {
            roleService.delete(roleVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","删除失败");
        }

        return new Result("success","删除成功");
    }

    /**
     * 更新角色信息
     * @param roleVo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody RoleVo roleVo){
        try {
            roleService.edit(roleVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error","编辑失败");
        }

        return new Result("success","编辑成功");
    }

    /**
     * 获取角色信息列表
     * @param roleVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result getList(@RequestBody RoleVo roleVo){
        PageResult pageResult = roleService.getList(roleVo.getKeyword(),roleVo.getCurrentPage(),roleVo.getPageSize());
        return new Result("success",pageResult);
    }
}
